/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package FrameUser;

import Database.CheckConnection;
import Database.UserDokumenTable;
import Model.Dokumen;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/**
 *
 * @author User
 */
public class UserLihatDokumen extends JFrame {
    private JTable tableDokumen;
    private ArrayList<Dokumen> data;
    private JTextField textterima;
    private JLabel label1, label2;
    private JButton buttonOk, buttonterima;

    public UserLihatDokumen(ArrayList<Dokumen> data1) {
        this.setTitle("Daftar Dokumen");
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setBounds(220, 150, 800, 500);
        this.setLayout(null);
        this.data = data1;
        
        tableDokumen = new JTable();
        tableDokumen.setBounds(25, 25, 730, 310);
        tableDokumen.setModel(new UserDokumenTable(data));
        this.add(tableDokumen);
        
        label1 = new JLabel("Dokumen Diterima :");
        label1.setBounds(320, 340, 120, 25);
        this.add(label1);
        
        label2 = new JLabel("Nomor Dokumen ");
        label2.setBounds(175, 370, 140, 25);
        this.add(label2);
        
        textterima = new JTextField();
        textterima.setBounds(280, 370, 200, 25);
        this.add(textterima);

        buttonterima = new JButton("Terima");
        buttonterima.setBounds(485, 370, 80, 25);
        this.add(buttonterima);
        buttonterima.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if(textterima.getText().equals("")){
                    JOptionPane.showMessageDialog(null, "Data belum diisi");
                } else {
                    data = new ArrayList<Dokumen>();
                    Connection con = CheckConnection.open("jdbc:oracle:thin:@localhost:1521:XE", "hr", "12345");
                    Statement stat = null;
                    String nomor = textterima.getText();
                    String masuk = "update DOKUMEN set DAFTAR_TERIMA = 'DITERIMA' where ID_PENERIMA in (select DATA from CACHE) and (NO_DOKUMEN = '"+nomor+"')";
                    try {
                        stat = con.createStatement();
                        stat.executeUpdate(masuk);
                    } catch (SQLException ex) {
                        Logger.getLogger(UserLihatDokumen.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    ResultSet hasil = null;
                    String sql = "select * from DOKUMEN where ID_PENERIMA in (select DATA from CACHE)";
                    try {
                        hasil = stat.executeQuery(sql);
                    } catch (SQLException ex) {
                        Logger.getLogger(UserLihatDokumen.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    try {
                        while (hasil.next()) {
                            Dokumen dokumen = new Dokumen();
                            try {
                                dokumen.setNomor_dokumen(hasil.getString("NO_DOKUMEN"));
                                dokumen.setPerihal(hasil.getString("PERIHAL"));
                                dokumen.setAsal_dokumen(hasil.getString("ASAL_DOKUMEN"));
                                dokumen.setPenerima_dokumen(hasil.getString("PENERIMA_DOKUMEN"));
                                dokumen.setId_penerima(hasil.getString("ID_PENERIMA"));
                                dokumen.setLetak_dokumen(hasil.getString("LETAK_DOKUMEN"));
                                dokumen.setPetugas_penerima(hasil.getString("PETUGAS_PENERIMA"));
                                dokumen.setTanggal_terima(hasil.getString("TANGGAL_TERIMA"));
                                dokumen.setTerima(hasil.getString("DAFTAR_TERIMA"));
                                data.add(dokumen);
                            } catch (Exception ex) {
                                Logger.getLogger(UserLihatDokumen.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                    } catch (SQLException ex) {
                        Logger.getLogger(UserLihatDokumen.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    textterima.setText(null);
                    JOptionPane.showMessageDialog(null, "Data Diterima");
                    setVisible(false);
                    UserLihatDokumen dosdok = new UserLihatDokumen(data);
                    dosdok.setVisible(true);
                }
            }
        });

        buttonOk = new JButton("OK");
        buttonOk.setBounds(335, 415, 80, 30);
        this.add(buttonOk);
        buttonOk.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
            }
        });
    }
    
    public ArrayList<Dokumen> getData() {
        return data;
    }

    public void setData(ArrayList<Dokumen> data) {
        this.data = data;
    }
}


